package com.kingyun.gpsinspection.purificationservice.services.jsm.innerservice.permission.util;

import com.cqhilink.api.common.utils.date.DateStyle;
import com.cqhilink.api.common.utils.date.DateUtil;
import com.kingyun.gpsinspection.purificationservice.common.bean.ApplicationContextHelper;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Roles;
import com.kingyun.gpsinspection.purificationservice.facade.model.webservicesmodel.UasRole;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.permission.JsmRolesService;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/2/17.
 */
public class RolesPool implements Runnable {
    private List<UasRole> list;
    private String controlType;//1.添加，2.修改，3.删除

    public void setList(List<UasRole> list) {
        this.list = list;
    }

    public void setControlType(String controlType) {
        this.controlType = controlType;
    }

    @Override
    public void run() {
        JsmRolesService rolesService = (JsmRolesService) ApplicationContextHelper.getBean("rolesService");
        if (rolesService != null && StringUtils.isNotEmpty(controlType)) {
            List<Roles> paramList = new ArrayList<Roles>();
            try {
                roles(list, paramList);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (paramList.size() > 0) {//角色信息处理
                if ("1".equals(controlType)) {
                    //添加
                    rolesService.insertList(paramList);
                } else if ("2".equals(controlType)) {
                    //修改
                    rolesService.updateList(paramList);
                } else if ("3".equals(controlType)) {
                    //删除
                    rolesService.deleteList(paramList);
                }
            }
        }
    }

    /**
     * 处理角色信息
     *
     * @param list
     * @param paramList
     * @throws Exception
     */
    private void roles(List<UasRole> list, List<Roles> paramList) throws Exception {
        if (list != null && list.size() > 0) {
            ConvertUtils.register(new DateConverter(null), Date.class);
            for (UasRole ur : list) {
                Roles param = new Roles();
                BeanUtils.copyProperties(param, ur);
                param.setCreatetime(DateUtil.StringToDate(ur.getCreateDate(), DateStyle.YYYY_MM_DD_HH_MM_SS));
                param.setModifyer(ur.getModifier());
                param.setState((short) 1);
                param.setModifytime(DateUtil.StringToDate(ur.getModifyDate(), DateStyle.YYYY_MM_DD_HH_MM_SS));
                param.setParentRoleId(ur.getRoleClassify());
                paramList.add(param);
            }
        }
    }
}
